<?php
/**
 * Created by PhpStorm.
 * User: N
 * Date: 2019/5/30
 * Time: 15:38
 */

namespace app\index\controller;


use app\index\model\doCate;
use app\index\model\doGoods;
use app\index\model\doUser;
use think\Db;
use think\facade\Request;

class Goods extends Base
{
    public function initialize()
    {
        parent::initialize();
    }

    // 商品列表
    public function goods()
    {
        $name = input('name');
        if (empty($name)){
            $where = [];
        }else{
            $where[] = ['name','like','%'.$name.'%'];
        }
        $goods = doGoods::where($where)
            ->paginate(10,false,['query' => request()->param()])->each(function ($item){
                $cate = doCate::get($item['cid']);
                $item['cate']=$cate['title'];
                //查询商品数量
                $ware_goods = Db::name("ware_goods")->where("gid",$item['id'])->select();
                $item['num'] = 0;
                foreach($ware_goods as $val){
                    $item['num'] += $val['num'];
                }

                return $item;
            });
//        dump($goods);die;
        $this->assign('page',$goods->render());
        $this->assign('goods',$goods);
        return $this->fetch();
    }

    // 增加商品
    public function addGoods()
    {
        if (Request::isPost()){
            $data = input('post.');
            $res = doGoods::where('name',$data['name'])->find();
            if ($res){
                return json(['code'=>0,'msg'=>'已存在同名商品']);
            }
           $p = Db::name('goods')->insert($data);
            if ($p){

                return json(['code'=>1,'msg'=>'增加成功']);
            }else{
                return json(['code'=>0,'msg'=>'网络开小差哦,稍后再试']);
            }
        }
        $cate = doCate::where('status',1)->select();
        $cate = $this->getTree($cate);
        for ($i=0;$i<sizeof($cate);$i++){
            $cate[$i]['title'] =  str_repeat('---', $cate[$i]['level']).$cate[$i]['title'];
        }
        $this->assign('cate',$cate);
        return $this->fetch();
    }
    // 下架商品
    public function disGoods()
    {
        $data = input('post.');
        if ($data['status'] == 1){
            $data['status'] = 2;
        }else{
            $data['status'] = 1;
        }
        $res = doGoods::update($data);
        if ($res){
            return json(['code'=>1]);
        }else{
            return json(['code'=>0]);
        }
    }

    // 编辑商品
    public function editGoods()
    {
        $id = input('id');
        if (Request::isPost()){
            $post = input('post.');
            if (doGoods::update($post)){
                return json(['code'=>1]);
            }else{
                return json(['code'=>0]);
            }
        }
        $goods = doGoods::get($id);
        $cate = doCate::where('status',1)->select();
        $cate = $this->getTree($cate);
        for ($i=0;$i<sizeof($cate);$i++){
            $cate[$i]['title'] =  str_repeat('---', $cate[$i]['level']).$cate[$i]['title'];
        }
        $this->assign('cate',$cate);
        $this->assign('goods',$goods);
        return  $this->fetch();
    }

    // 多分类列表
    public function cate()
    {
        $cate = doCate::all()->toArray();
        $cate = $this->getTree($cate);
        for ($i=0;$i<sizeof($cate);$i++){
            $cate[$i]['title'] =  str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-----', $cate[$i]['level']).$cate[$i]['title'];
        }

        $this->assign('cate',$cate);
        return $this->fetch();
    }


    // 递归分类
    function getTree($array, $pid =0, $level = 0){

        //声明静态数组,避免递归调用时,多次声明导致数组覆盖
        static $list = [];
        foreach ($array as $key => $value){
            //第一次遍历,找到父节点为根节点的节点 也就是pid=0的节点
            if ($value['pid'] == $pid){
                //父节点为根节点的节点,级别为0，也就是第一级
                $value['level'] = $level;
                //把数组放到list中
                $list[] = $value;
                //把这个节点从数组中移除,减少后续递归消耗
                unset($array[$key]);
                //开始递归,查找父ID为该节点ID的节点,级别则为原级别+1
                $this->getTree($array, $value['id'], $level+1);

            }
        }
        return $list;
    }

    // 添加分类
    public function addCate()
    {
        $post = input('post.');
        if (Request::isPost()){
            if (!empty($post['title'])){
                $c = new doCate();
                if ( $c->save($post)){
                    return json(['code'=>1,'msg'=>'添加成功']);
                }else{
                    return json(['code'=>2,'msg'=>'网络开小差了,请稍后再试']);
                }
            }else{
                return json(['code'=>2,'msg'=>'分类名称不能为空']);
            }
        }else{
            $this->assign('pid',input('pid'));
            return $this->fetch();
        }


    }


    // 禁用分类
    public function disCate()
    {
        $data = input('post.');
        if ($data['status'] == 1){
            $data['status'] = 2;
        }else{
            $data['status'] = 1;
        }
        $res = doCate::update($data);
        if ($res){
            return json(['code'=>1]);
        }else{
            return json(['code'=>0]);
        }
    }

    // 编辑分类
    public function editCate()
    {
        $id = input('id');
        if (Request::isPost()){
            $post = input('post.');
            if (doCate::update($post)){
                return json(['code'=>1]);
            }else{
                return json(['code'=>0]);
            }
        }else{
            $data = doCate::get($id);
            $this->assign('data',$data);
            return $this->fetch();
        }

    }
}